jQuery.noConflict();

var AcerUi = {};

AcerUi.debugMode = false;
AcerUi.$midLandFixer = null;
AcerUi.midLandFixerSelector = "#midLandFixer";

AcerUi.SMARTPHONE = "SMARTPHONE";
AcerUi.TABLET = "TABLET";
AcerUi.PC = "PC";

AcerUi._layouts = [
    { Id: AcerUi.SMARTPHONE, MaxWidth: 750 },
    { Id: AcerUi.TABLET, MaxWidth: 985 },
    { Id: AcerUi.PC, MaxWidth: 1024 }
];

AcerUi._initListeners = [];
AcerUi.addInitListener = function (eventCallBack)
{
    AcerUi._initListeners.push(eventCallBack);
};

AcerUi.init = function ()
{
    AcerUi.$midLandFixer = jQuery(AcerUi.midLandFixerSelector);

    AcerUi.addInitListener(new AcerUi.MainMenu().init);
	AcerUi.addInitListener(new AcerUi.FAQ().init);
    AcerUi.addInitListener(new AcerUi.BannerTopChange().init);
    AcerUi.addInitListener(new AcerUi.TabContent().init);
    AcerUi.addInitListener(new AcerUi.SimplePageMenu().init);
    AcerUi.addInitListener(new AcerUi.GoTop().init);
    AcerUi._currentLayout = AcerUi.getCurrentLayout();

    AcerUi.resize();
    window.onresize = AcerUi.resize;

    for (var i = 0; i < AcerUi._initListeners.length; i++)
    {
        AcerUi._initListeners[i](AcerUi._currentLayout);
    }

};

AcerUi._resizeListeners = [];
AcerUi.addResizeListener = function (eventCallBack)
{
    AcerUi._resizeListeners.push(eventCallBack);
};

AcerUi.resize = function ()
{
    var w = parseInt(jQuery(window).width(), 10);
    var h = parseInt(jQuery(window).height(), 10);

    for (var i = 0; i < AcerUi._resizeListeners.length; i++)
    {
        AcerUi._resizeListeners[i](w, h);
    }

    var layout = AcerUi.getCurrentLayout();
    if (layout.Id != AcerUi._currentLayout.Id)
    {

        var prevLayout = AcerUi._currentLayout;
        AcerUi._currentLayout = layout;
        AcerUi.changeLayout(layout, prevLayout);
    }
};

AcerUi._changeLayoutListeners = [];
AcerUi.addChangeLayoutListener = function (eventCallBack)
{
    AcerUi._changeLayoutListeners.push(eventCallBack);
};

AcerUi.changeLayout = function (currentLayout, prevLayout)
{
    for (var i = 0; i < AcerUi._changeLayoutListeners.length; i++)
    {
        AcerUi._changeLayoutListeners[i](currentLayout, prevLayout);
    }
};

AcerUi._currentLayout = null;
AcerUi.getCurrentLayout = function ()
{
    var refWidth = parseInt(AcerUi.$midLandFixer.width(), 10);
    var layout = null;
    for (var i = 0; i < AcerUi._layouts.length; i++)
    {
        if (refWidth <= AcerUi._layouts[i].MaxWidth)
        {
            return AcerUi._layouts[i];
            break;
        }
    }
    return AcerUi._layouts[AcerUi._layouts.length - 1];
};


/*MAIN MENU*/
AcerUi.MainMenu = function ()
{
    var that = this;
    var _$container = null;
    var _$mainMenu = null;
    var _$mainItems = null;
    var _$subMenus = null;
    var _$closeSubMenuButtons = null;
    var _$showNavigationButton = null;
    var _currentLayout = null;

    //constants
    var _activeClassName = "active";
    var _showDuration = "slow";
    var _hideDuration = "fast";

    this.init = function (layout)
    {
        //set start params
        _currentLayout = layout;

        //Calculate objects
        _$container = jQuery(jQuery(".header")[0]);
        _$mainMenu = jQuery(_$container.find(".main-menu")[0]);
        _$mainItems = jQuery(_$mainMenu.find("li > a"));
        _$subMenus = jQuery(_$container.find("> .content-submenu"));
        _$closeSubMenuButtons = jQuery(_$container.find(".content-submenu a.close"));
        _$showNavigationButton = jQuery(_$container.find(".btnShowMenu")[0]);

        //add objects eventhandlers
        _$mainItems.click(_toggleSubMenu);
        _$closeSubMenuButtons.live("click", _closeSubMenu);
        _$showNavigationButton.click(_toggleMainMenu);

        //register core events
        AcerUi.addResizeListener(that.resize);
        AcerUi.addChangeLayoutListener(that.layoutChange);
    };

    function _resetLayout(layout)
    {
        _currentLayout = layout;
        jQuery(jQuery(_$mainItems.parent()).find("div")).remove();
        _$subMenus.hide();
        _$mainItems.removeClass(_activeClassName);
        _$showNavigationButton.removeClass(_activeClassName);
        if (_currentLayout.Id == AcerUi.SMARTPHONE)
        {
            _$mainMenu.hide();
        }
        else
        {
            _$mainMenu.show();
        }
    };

    function _toggleSubMenu()
    {
        var $this = jQuery(this);
        $this.blur();

        //check if is an external link
        if ($this.data("sub") + "" != "true" || _currentLayout.Id == AcerUi.SMARTPHONE)
        {
            return true;
        }

        var ndx = _$mainItems.index($this);

        _$mainItems.removeClass(_activeClassName);
        $this.addClass(_activeClassName);

        var $visibleSubMenu = jQuery(_$subMenus.filter(":visible"));
        var visibleMenuNdx = _$subMenus.index($visibleSubMenu);
        var $subMenuToShow = jQuery(_$subMenus.get(ndx));

        if (visibleMenuNdx == ndx)
        {
            $subMenuToShow.slideUp(_hideDuration, function ()
            {
                $this.removeClass(_activeClassName);
            });
            return false;
        }

        var mainItemLeft = $this.position().left;

        if ($visibleSubMenu.length > 0)
        {
            $visibleSubMenu.hide();
            $subMenuToShow.width(_$container.width() - mainItemLeft);
            $subMenuToShow.css({ "padding-left": mainItemLeft }).show();
        }
        else
        {
            $subMenuToShow.width(_$container.width() - mainItemLeft);
            $subMenuToShow.css({ "padding-left": mainItemLeft }).slideDown(_showDuration);
        }
        return false;
    };

    function _closeSubMenu()
    {
        var $this = jQuery(this);
        var $subMenu = jQuery($this.parent());
        var $subMenuParent = jQuery($subMenu.parent());
        if ($subMenuParent.is("li"))
        {
            jQuery($subMenuParent.find("> a")[0]).click();
            return false;
        }
        var ndx = _$subMenus.index($subMenu);
        jQuery(_$mainItems.get(ndx)).click();
        return false;
    };

    function _toggleMainMenu()
    {
        var $this = jQuery(this);
        $this.blur();
        if (_$mainMenu.is(":visible"))
        {
            $this.removeClass(_activeClassName);
            _$mainMenu.slideUp(_hideDuration, function ()
            {
                _resetLayout(_currentLayout);
            });
        }
        else
        {
            $this.addClass(_activeClassName);
            _$mainMenu.slideDown(_showDuration);
        }
        return false;
    };

    this.resize = function ()
    {
        return;
    };
    this.layoutChange = function (currentLayout, prevLayout)
    {
        if (_currentLayout == null || _currentLayout.Id == currentLayout.Id)
            return;

        _resetLayout(currentLayout);
        return;
    };
};
/*MAIN FAQ*/
AcerUi.FAQ = function ()
{
    var that = this;
    var _$container = null;
    var _$questions = null;
    var _$answers = null;
    var _currentLayout = null;

    //constants
    var _activeClassName = "active";
    var _showDuration = "slow";
    var _hideDuration = "fast";

    this.init = function (layout)
    {
        //set start params
        _currentLayout = layout;

        //Calculate objects
        _$container = jQuery(jQuery(".faq"));
        _$questions = jQuery(_$container.find(".question"));
        _$answers = jQuery(_$container.find(".answer"));
        

        //add objects eventhandlers
		_$questions.on('click', function(event) {

				// Disable header links
				
				event.preventDefault();

				// Show and hide the tabs on click

				if (jQuery(this).attr('class') != 'active'){
					_$answers.slideUp('normal');
					jQuery(this).next().stop(true,true).slideToggle('normal');
					_$questions.removeClass('active');
					jQuery(this).addClass('active');
				}

			});

        //register core events
        AcerUi.addResizeListener(that.resize);
        AcerUi.addChangeLayoutListener(that.layoutChange);
    };
	this.resize = function ()
    {
        return;
    };
    this.layoutChange = function (currentLayout, prevLayout)
    {
        if (_currentLayout == null || _currentLayout.Id == currentLayout.Id)
            return;

        _resetLayout(currentLayout);
        return;
    };
};

/*Banner Header*/
AcerUi.BannerTopChange = function ()
{
    var that = this;
    var _imageClassName = null;

    this.init = function (layout)
    {
        _imageClassName = layout.Id.toLowerCase();
        jQuery(".banner-top").find("div").hide();
        jQuery(".banner-top").find("div." + _imageClassName).show();

        AcerUi.addChangeLayoutListener(that.layoutChange);
        _resetLayout(layout);
    };

    function _resetLayout(layout)
    {
        _imageClassName = layout.Id.toLowerCase();
        jQuery(".banner-top").find("div").hide();
        jQuery(".banner-top").find("div." + _imageClassName).show();
    };

    this.resize = function (currentLayout)
    {
        return;
    };

    this.layoutChange = function (currentLayout)
    {
        _resetLayout(currentLayout);
        return;
    };
};

AcerUi.TabContent = function ()
{
    var _activeClassName = "active";

    var _containerSelector = ".content-download";
    var _$container = null;

    var _tabLinkSelector = "> ul li a";
    var _$tabLinks = null;

    var _tabLinkParentSelector = "> ul li";
    var _$tabLinkParents = null;

    var _tabContainerSelector = ".content-tab > div";
    var _$tabContainers = null;

    this.init = function ()
    {
        /*Container check*/
        _$container = jQuery(_containerSelector);
        if (_$container.length == 0)
            return;

        _$container = jQuery(_$container[0]);
        _$tabLinks = jQuery(_$container.find(_tabLinkSelector));
        _$tabLinkParents = jQuery(_$container.find(_tabLinkParentSelector));
        _$tabContainers = jQuery(_$container.find(_tabContainerSelector));

        _$tabLinks.click(_toggleTab);
        _resetlayout();
    };

    function _resetlayout()
    {
        jQuery(_$tabLinks.get(0)).click();
    };

    function _toggleTab()
    {
        var $this = jQuery(this);
        var ndx = _$tabLinks.index($this);
        var $parent = jQuery(_$tabLinkParents.get(ndx));
        var $tabContainer = jQuery(_$tabContainers.get(ndx));

        if ($parent.hasClass(_activeClassName))
        {
            return false;
        }

        _$tabLinkParents.removeClass(_activeClassName);
        $parent.addClass(_activeClassName);

        _$tabContainers.hide();
        $tabContainer.show();
        return false;
    };
};


AcerUi.SimplePageMenu = function ()
{
    var that = this;
    var _currentLayout = null;

    var _activeClassName = "active";

    var _containerSelector = ".simple-page";
    var _$container = null;

    var _menuSelector = "ul.menu-content, div.menu-content";
    var _$menu = null;

    var _menuTogglerSelector = "a.btnShowMenu";
    var _$menuToggler = null;

    this.init = function (layout)
    {
        _currentLayout = layout;

        /*Container check*/
        _$container = jQuery(_containerSelector);
        if (_$container.length == 0)
            return;

        _$container = jQuery(_$container[0]);
        _$menu = jQuery(_$container.find(_menuSelector));
        _$menuToggler = jQuery(_$container.find(_menuTogglerSelector));

        /*events*/
        AcerUi.addChangeLayoutListener(that.layoutChange);
        _$menuToggler.click(_toggleMenu);

        /*inits*/
        _resetLayout(layout);
    };

    function _toggleMenu()
    {
        _$menuToggler.toggleClass(_activeClassName);
        _$menu.slideToggle();
        return false;
    };

    function _resetLayout(layout)
    {
        if (layout.Id == AcerUi.SMARTPHONE)
        {
            _$menu.hide();
            _$menuToggler.removeClass(_activeClassName).show();
        }
        else
        {
            _$menu.show();
            _$menuToggler.removeClass(_activeClassName).hide();
        }
    };

    this.resize = function (currentLayout)
    {
        return;
    };

    this.layoutChange = function (currentLayout)
    {
        if (_currentLayout == null || _currentLayout.Id == currentLayout.Id)
            return;
        _currentLayout = currentLayout;
        _resetLayout(currentLayout);
        return;
    };
};

AcerUi.GoTop = function ()
{
    var that = this;
    var _currentLayout = null;
    var _btnGoTopSelector = "a.go-top";
    var _$btnGoTop = null;

    this.init = function (layout)
    {
        _currentLayout = layout;

        _$btnGoTop = jQuery(_btnGoTopSelector);

        /*events*/
        AcerUi.addChangeLayoutListener(that.layoutChange);
        _$btnGoTop.click(_goTop);

        /*inits*/
        _resetLayout(layout);
    };

    function _goTop()
    {
        var pageUrl = document.location.href + "";
        var topUrl = document.location.href.split(document.location.hash).join("") + "#top";
        _$btnGoTop.attr("href", topUrl);
        return true;
    };

    function _resetLayout(layout)
    {
        return;
    };

    this.resize = function (currentLayout)
    {
        return;
    };

    this.layoutChange = function (currentLayout)
    {
        if (_currentLayout == null || _currentLayout.Id == currentLayout.Id)
            return;
        _currentLayout = currentLayout;
        return;
    };
};
